<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="./jquery.js"></script>
        <script type="text/javascript">
            jQuery(document).ready(function(){
                jQuery("form[name=form1] input[name=txtAlbumName]").blur(function(){
                    jQuery.ajax({
                        type:"GET",
                        data:"key="+$("form[name=form1] input[name=txtAlbumName]").val(),
                        url:"check_exist.php?type=2",
                        success:function(result){
                            $("#spAvailability").html(result);
                        }
                    });
                });
            });
                            
            
        </script>
    </head>
    <body>
        <?php
        require 'connect.inc';
        require 'session.inc';
        $res = "";
        $state = 0;
        $result;
        if ($_SESSION["isLogin"] == 0)
            header("Location: login.php");
        else {
            $iduser = $_SESSION['iduser'];
            if (isset($_POST["btnCreate"])) {
                $name = $_REQUEST["txtAlbumName"];
                $public = 1;
                if (isset($_REQUEST["chkPublic"]) && $_REQUEST["chkPublic"] != 'on')
                    $public = 0;
                $idUser = $_SESSION["iduser"];
                $avatar = "";
                $ext = "";
                $types = array('image/jpeg', 'image/gif', 'image/pjpeg');
                if (count($_FILES) > 0) {
                    
                    $file = $_FILES['fAvatar'];
                    if (!in_array($file['type'], $types)) {
                        return;
                    }
                    if ($file['error'] == 0) {
                        $avatar = $file['name'];
                        $temp = pathinfo($file["name"]);
                        $ext = $temp['extension'];
                    }
                }
                $strSQL = "SELECT * FROM album WHERE album_name = '$name'";
              
                $result = mysql_query($strSQL);
                if ($result && mysql_num_rows($result) == 0) {
                    $strSQL = "INSERT INTO `album` (`album_name`, `owner`, `date_created`, `date_modified`, `public`, `avatar`)
                    VALUES ('$name', $idUser, now(), now(), $public, '$avatar')";
                   
                    $result = mysql_query($strSQL);
                    $idAlbum = mysql_insert_id();
                   
                    if ($result)
                    //Create user's folder and upload file
                        if (mkdir("upload/$idUser/$idAlbum", 0777))
                            if (mkdir("upload/$idUser/$idAlbum/avatar", 0777)) {
                                if ($avatar != "") {
                                    $strSQL = "INSERT INTO `photo` (`album_id`, `photo_name`, `date_created`, `date_modified`, `image`)
                                            VALUES ($idAlbum, '$avatar', now(), now(), '$avatar')";
                                    echo $strSQL;
                                    $result = mysql_query($strSQL);
                                    $idphoto = mysql_insert_id();
                                    if ($result) {
                                        mysql_query("UPDATE PHOTO SET image = '$idphoto.$ext' WHERE photo_id = $idphoto");
                                        mysql_query("UPDATE ALBUM SET avatar = '$idphoto.$ext' WHERE album_id = $idAlbum");
                                        move_uploaded_file($file['tmp_name'], "upload/$idUser/$idAlbum/avatar/$idphoto.$ext");
                                        move_uploaded_file($file['tmp_name'], "upload/$idUser/$idAlbum/$idphoto.$ext");
                                    }
                                }
                            }
                }
            }
            $res .=
                    "<table width='100%' border='1' cellspacing='0'>
                    <tbody>
                        <tr></tr>
                        <tr>
                            <td>
                                <img src='logo.jpg'>
                            </td>
                            <td>
                                <h1>Photo Album Website</h1>
                            </td>
                        </tr>
                        <tr>
                            <td width='20%' valign='top'>
                                <a href='index.php'>Home</a>
                                <br>
                                <a href='my_albums.php'>My Albums</a>
                                <br>
                                <a href='users.php'>Other persons</a>
                            </td>
                            <td>
                                <center>
                                <h2>Create album</h2>
                            </center>
                           <form enctype='multipart/form-data' onsubmit = 'return check_Validation();' method='post' action='my_albums.php' name='form1'>
                            <table>
                                <tbody>
                                    <tr>
                                        <td>

                                        </td>
                                    </tr>
                                    <tr>
                                        <td>Album name: </td>
                                        <td>
                                            <input type='text' value='' name='txtAlbumName'>
                                            <span id='spAvailability' style='color: red'>
                                                <font style='color:red'></font>
                                            </span>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>Avatar: </td>
                                        <td>
                                            <input type='file' value='' name='fAvatar'>
                                        </td>
                                    </tr>
                                    <tr>
                                    <td>Public: </td>
                                        <td>
                                            <input type='checkbox' name='chkPublic'>

                                        </td>
                                    </tr>
                                    <tr>
                                    <td></td>
                                        <td>
                                            <input type='submit' value='Create' name='btnCreate'>
                                            <input type='reset' name='btnCancel' value='Cancel'>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </form>
                        <br/><hr>
                            My albums:
                         <br>";
            $strSQL = "SELECT a.`album_id`,a.`album_name`, a.`avatar`
                    FROM `album` a
                    WHERE a.`owner` = $iduser";
            $result = mysql_query($strSQL);
            if ($result) {
                if (mysql_num_rows($result) < 1)
                    $res .= "No album!";
                else
                    while ($row = mysql_fetch_array($result)) {
                        $idAlbum = $row["album_id"];
                        $avatar = $row["avatar"];
                        $name = $row["album_name"];
                        $res .= "<table style='float:left;'>
                                <tbody>
                                    <tr>
                                        <td>
                                            <a href='edit_album.php?album_id=$idAlbum'>Edit</a>
                                                |
                                            <a href='view_album.php?album_id=$idAlbum'>View</a>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <img height='100' src='./upload/$iduser/$idAlbum/avatar/$avatar'>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <a href='view_album.php?user_id=$iduser&album_id=$idAlbum'>$name</a>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>";
                    }
            }
            // User logged in

            $res .= " <td width='20%' valign='top'>
                        Wellcom <a href='#'>" . $_SESSION["username"] . "</a>!
                        |
                        <a href='logout.php'>Logout</a></td>
                    </tr>
                </tbody>
                </table>";
            echo $res;
        }
        ?>
    </body>


</html>
